package com.learn.lb.mapred;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * @author laibo
 * @since 2019/7/8 10:46
 * ===================================
 * <p> 求共同好友
 * <p>
 * <p>    1) 源数据格式：A:B,C,D,E,F,G
 * <p>                   B:C,E,G
 * <p>                   C:A,E
 * <p>    2) 输出：A_B:E,G
 * <p>             A_C:E
 * <p>             B_C:E
 * ===================================
 */
public final class CommonFriendTask {

    public static class Map extends Mapper<Text, Text, Text, Text> {
        @Override
        protected void map(Text key, Text value, Context context) throws IOException, InterruptedException {
            String[] lineSplit = value.toString().trim().split(":");
            String user = lineSplit[0];
            if (lineSplit.length < 2) {
                return;
            }
            //获取朋友列表
            String[] friends = lineSplit[1].trim().split(",");
            for (String friend : friends) {
                context.write(new Text(friend), new Text(user));
            }
        }
    }

    public static class Red extends Reducer<Text, Text, Text, Text> {

        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            StringBuilder keyBuilder = new StringBuilder();
            for (Text value : values) {

            }
        }
    }
}
